import xlrd
import winreg


# 获取桌面路径
def get_desktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
    return winreg.QueryValueEx(key, "Desktop")[0]


serial_number = input("请输入本期开始的编号：")
serial_number_prefix = serial_number[0:len(serial_number)-3] # 编号前缀

# print(serial_number[len(serial_number)-3:])
# print(str((int(serial_number[len(serial_number)-3:])+1)).zfill(3))

workbook = xlrd.open_workbook('C:\\Users\\cjf\\Desktop\\zssise.xlsx')
# print(workbook.sheet_names())  # 查看所有sheet
booksheet = workbook.sheet_by_index(0)  # 用索引取第一个sheet
# booksheet = workbook.sheet_by_name('Sheet 1')  # 或用名称取sheet

#  读单元格数据
xlsxrows = booksheet.nrows # 行数
xlsxcols = booksheet.ncols # 列数

desktopPath = get_desktop()

saveText = open(desktopPath + "\\zssise.txt","w",encoding='utf-8')      # 保存卖舍友的内容
saveWechatId = open(desktopPath + "\\keyword.txt","w",encoding='utf-8') # 保存微信
saveText.write("\n")

# 处理内容
def handlecontent():
    i = 1
    j = 0
    try:
     num = int(serial_number[len(serial_number) - 3:])
    except ValueError:
        print("请检查输入的编号是否正确；(正确格式：R5TH001)")
        return
    while i < xlsxrows:
        if i == 1:  # 第二行不显示
            i += 1
            j = 0
        else:
            new_serial_number = serial_number_prefix + str((num)).zfill(3) # 组装好的编号
            while j < xlsxcols:
                if j == 1 or j == 3:  # 第一列发布时间不显示,第三列性别不显示
                    j += 1
                elif j == 6:
                    saveWechatId.write(new_serial_number + "**" + (booksheet.cell_value(i,j).replace("\n","")) + "**")
                    j += 1
                elif j == 7:
                    # saveWechatId.write(repr(booksheet.cell_value(i, j)) + "\n")
                    saveWechatId.write((booksheet.cell_value(i, j).replace("\n","")) + "\n")
                    j += 1
                else:
                    cell_item = booksheet.cell_value(i, j)
                    if cell_item == "": # 遇到空行，表示没有内容，直接结束。
                        print("处理完毕，共"+ str(i-2) + "人")
                        return
                    if j == 0:           # 保存图片的名字
                        cell_item = "图片编号（"+ (str(i-1)) + "）："+ cell_item
                    # print(cell_item)
                    j += 1
                    saveText.write(cell_item + "\n\n")
            i += 1
            j = 0
            saveText.write(new_serial_number + "\n\n")
            saveText.write("--------------------------------------------\n\n\n")
            num += 1


handlecontent()

# 关闭文件
saveText.close()
saveWechatId.close()
